﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.EntityClient;
using CongregacaoOs.Data;

namespace CongregacaoOs.Business
{
    [System.ComponentModel.DataObject]
    public class ccPostBus
    {
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public bool PostSaveOrUpdate(ccPost post)
        {
            if (post.texto == null) { throw new Exception("O texto não foi informado"); }

            using (EntityConnection conn = new EntityConnection("name = CcbOrdemServico"))
            {
                try
                {
                    using (EntityCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "CcbOrdemServico.ccPostSave";
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;

                        EntityParameter param;

                        param = cmd.Parameters.Add("IDPOST", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (post.idPost != 0)
                            param.Value = post.idPost;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("IDOS", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (post.idOs != 0)
                            param.Value = post.idOs;
                        else
                            param.Value = DBNull.Value;


                        param = cmd.Parameters.Add("TEXTO", DbType.String);
                        param.Direction = ParameterDirection.Input;
                        if (post.texto != "")
                            param.Value = post.texto;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("TITULO", DbType.String);
                        param.Direction = ParameterDirection.Input;
                        if (post.titulo != "")
                            param.Value = post.titulo;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("DATACRIACAO", DbType.DateTime);
                        param.Direction = ParameterDirection.Input;
                        if (post.dataCriacao != DateTime.MinValue)
                            param.Value = post.dataCriacao;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("IDSERVO", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (post.idServo != 0)
                            param.Value = post.idServo;
                        else
                            param.Value = DBNull.Value;

                        using (conn)
                        {
                            if (conn.State == ConnectionState.Closed)
                                conn.Open();

                            cmd.ExecuteNonQuery();
                            return true;
                        }
                    }
                }
                catch (Exception)
                {
                    return false;
                }

            }
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public List<ccPostSelect_Result> PostSelect(int aIdOs)
        {
            using (EntityConnection conn = new EntityConnection("name = CcbOrdemServico"))
            {
                try
                {
                    using (EntityCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "CcbOrdemServico.ccPostSelect";
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;

                        EntityParameter param;


                        param = cmd.Parameters.Add("IDPOST", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("IDOS", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (aIdOs != 0)
                            param.Value =aIdOs;
                        else
                            param.Value = DBNull.Value;

                        using (conn)
                        {
                            if(conn.State == ConnectionState.Closed)conn.Open();
                            using (EntityDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
                            {

                                List<ccPostSelect_Result> _result = new List<ccPostSelect_Result>();
                                while (reader.Read())
                                {
                                    ccPostSelect_Result _post = new ccPostSelect_Result();
                                    _post.idPost = reader.IsDBNull(reader.GetOrdinal("idPost")) == false ? reader.GetInt32(reader.GetOrdinal("idPost")) : 0;
                                    _post.texto = reader.IsDBNull(reader.GetOrdinal("texto")) == false ? reader.GetString(reader.GetOrdinal("texto")) : "";
                                    _post.titulo = reader.IsDBNull(reader.GetOrdinal("titulo")) == false ? reader.GetString(reader.GetOrdinal("titulo")) : "";
                                    _post.dataCriacao = reader.IsDBNull(reader.GetOrdinal("dataCriacao")) == false ? reader.GetDateTime(reader.GetOrdinal("dataCriacao")) : DateTime.MinValue;
                                    _post.idOs = reader.IsDBNull(reader.GetOrdinal("idOs")) == false ? reader.GetInt32(reader.GetOrdinal("idOs")) : 0;                                    
                                    _post.idServo = reader.IsDBNull(reader.GetOrdinal("idServo")) == false ? reader.GetInt32(reader.GetOrdinal("idServo")) : 0;
                                    _post.NomeServo = reader.IsDBNull(reader.GetOrdinal("NomeServo")) == false ? reader.GetString(reader.GetOrdinal("NomeServo")) : "";
                                    _result.Add(_post);
                                }
                                return _result;
                            }
                        }


                    }
                }
                catch (Exception)
                {
                    return null;
                }

            }
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public void PostDelete(int id)
        {
            try
            {
                using (CcbOrdemServico objDB = new CcbOrdemServico())
                {
                    objDB.ccPostDelete(id);
                }
            }
            catch (System.Exception)
            {

                throw;
            }
        }
    }
}
